package jxc.servlet.info.customer;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxc.base.helper.IdGenerator;
import jxc.data.hibernate.dao.CustomerDAO;
import jxc.data.hibernate.pojo.Customer;

/**
 * 添加或更新客户信息
 */
@WebServlet("/info/customer/update")
public class Update extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * 获取更新页面
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		CustomerDAO dao=new CustomerDAO();
		Customer customer=null;
		if(id!=null)
			customer=dao.getById(id);
		if(customer==null)
			customer=new Customer(IdGenerator.getCustomerId(),"",1);
		request.setAttribute("c", customer);
		request.getRequestDispatcher("/info/customer/update.jsp").forward(request, response);
	}

	/**
	 * 更新商品
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		String name=request.getParameter("name");
		String zip=request.getParameter("zip");
		String address=request.getParameter("address");
		String tel=request.getParameter("tel");
		String linkman=request.getParameter("linkman");
		String phone=request.getParameter("phone");
		String bank=request.getParameter("bank");
		String account=request.getParameter("account");
		String email=request.getParameter("email");
		String fax=request.getParameter("fax");
		CustomerDAO dao=new CustomerDAO();
		Customer customer=new Customer(id, name, zip, address, tel, linkman, phone, bank, account, email, fax, 1, null,null);
		dao.saveOrUpdate(customer);
		dao.commit();
		request.setAttribute("message", "更新客户信息成功");
		this.doGet(request, response);
	}

}
